﻿namespace SLClient.ViewModels
{
    using SLClient;
    using SLClient.CustomerControls;
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using SLClient.Views;
    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Threading;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;

    public class MoneyExportViewModel : ViewModelBase
    {
        private int _amount;
        private string _confirmAmountPwd;
        private DispatcherTimer _dispatcherTimer;
        private string _errMsg;
        private DateTime _invailTime;
        private bool _isEnable;
        private UserBank _loginUserBank;
        private readonly MoneyExportView _moneyExportView;
        private MoneyInHistory _moneyInHistory;
        private Visibility _moneyInVisibility;
        private Visibility _moneyOutVisibility;
        private Visibility _processVisibity;
        private Visibility _refreshVisibity;
        private string _spanTime;
        private const string Msg = "未设定银行资料!";

        public MoneyExportViewModel(MoneyExportView moneyExportView)
        {
            this._moneyExportView = moneyExportView;
            this._moneyInHistory = new MoneyInHistory();
            this._loginUserBank = new UserBank();
            this._isEnable = false;
            this._invailTime = DateTime.Now.AddMinutes(5.0);
            this._dispatcherTimer = new DispatcherTimer();
            this._dispatcherTimer.Interval=TimeSpan.FromMilliseconds(1000.0);
            this._dispatcherTimer.Tick+=new EventHandler(this.DispatcherTimerTick);
            this._moneyOutVisibility = Visibility.Visible;
            this._moneyInVisibility = Visibility.Collapsed;
            this._refreshVisibity = Visibility.Collapsed;
            this._processVisibity = Visibility.Collapsed;
            this.CommandInit();
            this._dispatcherTimer.Start();
            this.LoadUserBank();
        }

        private void CommandInit()
        {
            RelayCommand command = new RelayCommand(this.Transition) {
                IsEnabled = true
            };
            this.TransitionCommand = command;
            RelayCommand command2 = new RelayCommand(this.ShowMoneyDemo) {
                IsEnabled = true
            };
            this.ShowMoneyDemoCommand = command2;
            RelayCommand command3 = new RelayCommand(this.CopyBankAccount) {
                IsEnabled = true
            };
            this.CopyBankAccountCommand = command3;
            RelayCommand command4 = new RelayCommand(this.CopyUserId) {
                IsEnabled = true
            };
            this.CopyOrderIdCommand = command4;
            RelayCommand command5 = new RelayCommand(this.CopyAccountName) {
                IsEnabled = true
            };
            this.CopyAccountNameCommand = command5;
            RelayCommand command6 = new RelayCommand(this.Refresh) {
                IsEnabled = true
            };
            this.RefreshTimeCommand = command6;
            RelayCommand command7 = new RelayCommand(this.Commit) {
                IsEnabled = true
            };
            this.CommitComand = command7;
        }

        public void Commit()
        {
            MoneyOutHistory moneyOutHistory;
            if (this.Amount < ViewDispatcher.CurrentConfig.MinMoneyOut)
            {
                this.ErrMsg = "低于最小提现金额！";
            }
            else if (string.IsNullOrEmpty(this.ConfirmAmountPwd) || string.IsNullOrWhiteSpace(this.ConfirmAmountPwd))
            {
                this.ErrMsg = "验证密码不能为空！";
            }
            else if (this.ConfirmAmountPwd != ViewDispatcher.CurrentUserBank.AccountPwd)
            {
                this.ErrMsg = "资金密码错误！";
            }
            else if ((this.Amount - ViewDispatcher.LoginUser.ActivityMoney) > 0M)
            {
                this.ErrMsg = "提取金额大于账户可用余额！";
            }
            else
            {
                this.ErrMsg = "";
                this.ProcessVisibity = 0;
                moneyOutHistory = new MoneyOutHistory {
                    UserId = ViewDispatcher.LoginUser.UserId,
                    CreateTime = DateTime.Now,
                    Amount = this.Amount
                };
                ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
                instance.CreateMoneyOutApplyCompleted += delegate (object obj, CreateMoneyOutApplyCompletedEventArgs arg) {
                    this.ProcessVisibity = Visibility.Collapsed;
                    if (arg.Result == 6)
                    {
                        this.ErrMsg = "提取系统发生故障，请联系客服";
                    }
                    else if (arg.Result == 1)
                    {
                        this.ErrMsg = "提取金额大于账户可用余额";
                    }
                    else if (arg.Result == 2)
                    {
                        this.ErrMsg = "提现时间超过系统设定时间";
                    }
                    else if (arg.Result == 3)
                    {
                        this.ErrMsg = "今日提现次数已超过系统限制";
                    }
                    else if (arg.Result == 4)
                    {
                        this.ErrMsg = "今日提现总额已超过系统限制";
                    }
                    else if (arg.Result == 5)
                    {
                        App.Show("已提交系统处理，请稍候查看");
                        this._moneyExportView.Close();
                        UserInfo loginUser = ViewDispatcher.LoginUser;
                        loginUser.ActivityMoney -= moneyOutHistory.Amount;
                        UserInfo info2 = ViewDispatcher.LoginUser;
                        info2.FreezeMoney += moneyOutHistory.Amount;
                        ViewDispatcher.LoginUser.TotleMoney = ViewDispatcher.LoginUser.ActivityMoney + ViewDispatcher.LoginUser.FreezeMoney;
                    }
                };
                instance.CreateMoneyOutApplyAsync(moneyOutHistory);
            }
        }

        public void CopyAccountName()
        {
            try
            {
                Clipboard.SetText(this.CurrentMoneyInHistory.SysAccountName);
            }
            catch (Exception)
            {
                App.Show("无法访问剪切板");
            }
        }

        public void CopyBankAccount()
        {
            try
            {
                Clipboard.SetText(this.CurrentMoneyInHistory.SysBankNumber);
            }
            catch (Exception)
            {
                App.Show("无法访问剪切板");
            }
        }

        public void CopyUserId()
        {
            try
            {
                Clipboard.SetText(this.CurrentMoneyInHistory.OrderId);
            }
            catch (Exception)
            {
                App.Show("无法访问剪切板");
            }
        }

        private void DispatcherTimerTick(object sender, EventArgs e)
        {
            TimeSpan span = (TimeSpan) (this._invailTime - DateTime.Now);
            if (span.TotalSeconds < 1.0)
            {
                this._dispatcherTimer.Stop();
            }
            this.SpanTime = span.ToString("mm") + ":" + span.ToString("ss");
        }

        public void Load()
        {
            this._invailTime = DateTime.Now.AddMinutes(5.0);
            this.ProcessVisibity = 0;
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetLatestSysBankInfoCompleted += delegate (object obj, GetLatestSysBankInfoCompletedEventArgs arg) {
                if (arg.Result == null)
                {
                    this.CurrentMoneyInHistory = new MoneyInHistory();
                }
                this.CurrentMoneyInHistory = arg.Result;
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetLatestSysBankInfoAsync(ViewDispatcher.LoginUser.UserId, DateTime.Now);
        }

        private void LoadUserBank()
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetBankInfoByUserIdCompleted += delegate (object obj, GetBankInfoByUserIdCompletedEventArgs arg) {
                if (arg.Result == null)
                {
                    this.ErrMsg = "未设定银行资料!";
                }
                else
                {
                    this.IsEnable = true;
                    this.LoginUserBank = arg.Result;
                    this.LoginUserBank.AccountPwd = DesHelper.DesDecrypt(arg.Result.AccountPwd);
                    UserBankViewModel.Cache(this.LoginUserBank);
                }
            };
            if (!IsolatedStorageHelper<ObservableCollection<UserBank>>.IsSettingPersisted("CurrentUserBank"))
            {
                instance.GetBankInfoByUserIdAsync(ViewDispatcher.LoginUser.UserId);
            }
            else
            {
                this.LoginUserBank = ViewDispatcher.CurrentUserBank;
                if (string.IsNullOrEmpty(ViewDispatcher.CurrentUserBank.AccountName) || string.IsNullOrEmpty(ViewDispatcher.CurrentUserBank.BankNumber))
                {
                    this.ErrMsg = "未设定银行资料!";
                }
                else
                {
                    this.IsEnable = true;
                }
            }
        }

        public void Refresh()
        {
            this.Load();
        }

        public void ShowMoneyDemo()
        {
            new MoneyDempWindow().Show();
        }

        public void Transition()
        {
            if (this.ErrMsg != "未设定银行资料!")
            {
                this.ErrMsg = "";
            }
            if (this._moneyOutVisibility == Visibility.Collapsed)
            {
                this.MoneyOutVisibility = Visibility.Visible;
                this.MoneyInVisibility = Visibility.Collapsed;
                this.RefreshVisibity = Visibility.Collapsed;
                this.Amount = 0;
                this.ConfirmAmountPwd = "";
            }
            else
            {
                this.MoneyOutVisibility = Visibility.Collapsed;
                this.MoneyInVisibility = Visibility.Visible;
                this.RefreshVisibity = Visibility.Visible;
                this.Load();
            }
        }

        public int Amount
        {
            get
            {
                return this._amount;
            }
            set
            {
                this._amount = value;
                base.Notify<int>(() => this.Amount);
                //base.Notify<int>(System.Linq.Expressions.Expression.Lambda<Func<int>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_Amount)), new ParameterExpression[0]));
            }
        }

        public ICommand CommitComand { get; set; }

        public string ConfirmAmountPwd
        {
            get
            {
                return this._confirmAmountPwd;
            }
            set
            {
                this._confirmAmountPwd = value;
                base.Notify<string>(() => this.ConfirmAmountPwd);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_ConfirmAmountPwd)), new ParameterExpression[0]));
            }
        }

        public ICommand CopyAccountNameCommand { get; set; }

        public ICommand CopyBankAccountCommand { get; set; }

        public ICommand CopyOrderIdCommand { get; set; }

        public MoneyInHistory CurrentMoneyInHistory
        {
            get
            {
                return this._moneyInHistory;
            }
            set
            {
                this._moneyInHistory = value;
                base.Notify<MoneyInHistory>(() => this.CurrentMoneyInHistory);
                //base.Notify<MoneyInHistory>(System.Linq.Expressions.Expression.Lambda<Func<MoneyInHistory>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_CurrentMoneyInHistory)), new ParameterExpression[0]));
            }
        }

        public string ErrMsg
        {
            get
            {
                return this._errMsg;
            }
            set
            {
                this._errMsg = value;
                base.Notify<string>(() => this.ErrMsg);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_ErrMsg)), new ParameterExpression[0]));
            }
        }

        public bool IsEnable
        {
            get
            {
                return this._isEnable;
            }
            set
            {
                this._isEnable = value;
                base.Notify<bool>(() => this.IsEnable);
                //base.Notify<bool>(System.Linq.Expressions.Expression.Lambda<Func<bool>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_IsEnable)), new ParameterExpression[0]));
            }
        }

        public UserBank LoginUserBank
        {
            get
            {
                return this._loginUserBank;
            }
            set
            {
                this._loginUserBank = value;
                base.Notify<UserBank>(() => this.LoginUserBank);
                //base.Notify<UserBank>(System.Linq.Expressions.Expression.Lambda<Func<UserBank>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_LoginUserBank)), new ParameterExpression[0]));
            }
        }

        public string MinMoneyOut
        {
            get
            {
                return string.Format("最小提现金额{0:F0}元", ViewDispatcher.CurrentConfig.MinMoneyOut.Value);
            }
        }

        public Visibility MoneyInVisibility
        {
            get
            {
                return this._moneyInVisibility;
            }
            set
            {
                this._moneyInVisibility = value;
                base.Notify<Visibility>(() => this.MoneyInVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_MoneyInVisibility)), new ParameterExpression[0]));
            }
        }

        public string MoneyOutCount
        {
            get
            {
                return string.Format("同一银行账户每日最多提现{0}次", ViewDispatcher.CurrentConfig.MoneyOutCount);
            }
        }

        public Visibility MoneyOutVisibility
        {
            get
            {
                return this._moneyOutVisibility;
            }
            set
            {
                this._moneyOutVisibility = value;
                base.Notify<Visibility>(() => this.MoneyOutVisibility);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_MoneyOutVisibility)), new ParameterExpression[0]));
            }
        }

        public Visibility ProcessVisibity
        {
            get
            {
                return this._processVisibity;
            }
            set
            {
                this._processVisibity = value;
                base.Notify<Visibility>(() => this.ProcessVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_ProcessVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand RefreshTimeCommand { get; set; }

        public Visibility RefreshVisibity
        {
            get
            {
                return this._refreshVisibity;
            }
            set
            {
                this._refreshVisibity = value;
                base.Notify<Visibility>(() => this.RefreshVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_RefreshVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand ShowMoneyDemoCommand { get; set; }

        public string SpanTime
        {
            get
            {
                return this._spanTime;
            }
            set
            {
                this._spanTime = value;
                base.Notify<string>(() => this.SpanTime);
                //base.Notify<string>(System.Linq.Expressions.Expression.Lambda<Func<string>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MoneyExportViewModel)), (MethodInfo)(MoneyExportViewModel.get_SpanTime)), new ParameterExpression[0]));
            }
        }

        public ICommand TransitionCommand { get; set; }
    }
}

